from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
from GUI.ListenWorker import ListenWorker
from test.MainGUI import Ui_Dialog


class MyMainWindow(QDialog, Ui_Dialog):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        # 初始化变量
        self.button_status = 1
        self._translate = QCoreApplication.translate
        self.worker = ListenWorker()
        self.setWindowFlags(Qt.WindowStaysOnTopHint)    # 设置窗口在最前面
        self.bind_event()


    def bind_event(self):
        # 绑定各种事件
        # self.pushButton.clicked.connect(self.start_button_event)       # 开始按钮
        self.worker.sinOut.connect(self.add_stock)               # 收到信息
        # self.listWidget.itemDoubleClicked.connect(self.auto_gui)
        # self.listWidget.itemClicked.connect(self.auto_gui)


    def add_stock(self, stock_info):  # 增加股票代码到列表中
        self.listWidget.addItem(stock_info)

    def start_button_event(self, event):
        if self.button_status == 1:         # 启动事件
            self.send_queue.put(True)
            msg = self.rece_queue.get(block=True, timeout=None)
            if msg:                         # 启动成功
                print('**GUIController** start successfully')
                self.button_status = 0
                self.worker.my_start()
                self.pushButton.setText(self._translate("Dialog", "暂停"))
        else:                               # 暂停事件
            self.send_queue.put(False)
            self.button_status = 1
            self.worker.my_stop()
            self.pushButton.setText(self._translate("Dialog", "开始"))

    def closeEvent(self, event):
        """
        重写closeEvent方法，实现dialog窗体关闭时执行一些代码
        :param event: close()触发的事件
        :return: None
        """
        self.send_queue.put('end')
        exit()
        event.accept()

    '''重载一下鼠标按下事件(单击)'''
    def mousePressEvent(self, event):
        if event.buttons() == Qt.LeftButton:                           # 左键按下
            event.accept()
        elif event.buttons () == Qt.RightButton:                        # 右键按下
            pass
        elif event.buttons () == Qt.MidButton:                          # 中键按下
            pass
        elif event.buttons () == Qt.LeftButton | QtCore.Qt.RightButton: # 左右键同时按下
            pass
        elif event.buttons () == Qt.LeftButton | QtCore.Qt.MidButton:   # 左中键同时按下
            pass
        elif event.buttons () == Qt.MidButton | QtCore.Qt.RightButton:  # 右中键同时按下
            pass
        elif event.buttons () == Qt.LeftButton | QtCore.Qt.MidButton \
             | QtCore.Qt.RightButton:                                          # 左中右键同时按下
            pass


def run_gui():
    app = QApplication(sys.argv)
    app.addLibraryPath('./ plugins')
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    run_gui()

